﻿SET NUMERIC_ROUNDABORT OFF
GO
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id=OBJECT_ID('tempdb..#tmpErrors')) DROP TABLE #tmpErrors
GO
CREATE TABLE #tmpErrors (Error int)
GO
SET XACT_ABORT ON
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO
BEGIN TRANSACTION
GO
PRINT N'Creating [dbo].[Nodes]'
GO
CREATE TABLE [dbo].[Nodes]
(
[ID] [int] NOT NULL IDENTITY(1, 1),
[ParentID] [int] NULL,
[Type] [nvarchar] (50) COLLATE Cyrillic_General_CI_AS NOT NULL,
[Title] [nvarchar] (max) COLLATE Cyrillic_General_CI_AS NOT NULL
)

GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_Nodes] on [dbo].[Nodes]'
GO
ALTER TABLE [dbo].[Nodes] ADD CONSTRAINT [PK_Nodes] PRIMARY KEY CLUSTERED ([ID])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
IF EXISTS (SELECT * FROM #tmpErrors) ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT>0 BEGIN
PRINT 'The database update succeeded'
COMMIT TRANSACTION
END
ELSE PRINT 'The database update failed'
GO
DROP TABLE #tmpErrors
GO


SET NUMERIC_ROUNDABORT OFF
GO
SET XACT_ABORT, ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS, NOCOUNT ON
GO
SET DATEFORMAT YMD
GO
-- Pointer used for text / image updates. This might not be needed, but is declared here just in case
DECLARE @pv binary(16)

BEGIN TRANSACTION

-- Add 5 rows to [dbo].[Nodes]
SET IDENTITY_INSERT [dbo].[Nodes] ON
INSERT INTO [dbo].[Nodes] ([ID], [ParentID], [Type], [Title]) VALUES (1, NULL, N'drive', N'root')
INSERT INTO [dbo].[Nodes] ([ID], [ParentID], [Type], [Title]) VALUES (3, 1, N'folder', N'123')
INSERT INTO [dbo].[Nodes] ([ID], [ParentID], [Type], [Title]) VALUES (4, 1, N'folder', N'12333')
INSERT INTO [dbo].[Nodes] ([ID], [ParentID], [Type], [Title]) VALUES (12, 1, N'folder', N'123')
INSERT INTO [dbo].[Nodes] ([ID], [ParentID], [Type], [Title]) VALUES (13, 4, N'file', N'123')
SET IDENTITY_INSERT [dbo].[Nodes] OFF

COMMIT TRANSACTION
GO